<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            v-for="(item, index) in options1"
            :key="item.id"
            :class="{ active: index == op1Val }"
            @click="op1Fn(index, item.min, item.max)"
            >{{ item.txt }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            v-for="(item, index) in options2"
            :key="item.id"
            :class="{ active: index == op2Val }"
            @click="op2Fn(index, item.type)"
            >{{ item.txt }}</span
          >
        </li>
      </ul>
      <List :arr="goodsListShow" />
      <div style="text-align: center">
        {{ isReachBottom ? "数据已经加载完" : "正在加载中..." }}
      </div>
    </div>
  </div>
</template>
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { allGoodsAPI } from "@/request/api";
import { getScrollTop, getClientHeight, getScrollHeight } from "@/store/utils";
export default {
  data() {
    return {
      // 栏目 1->精品推荐 2-> 热门兑换 0->全部
      did: 0,
      // 1->实体商品 2->虚拟商品 0->全部
      type: 0,
      // 积分范围的最小值  大于多少积分，最少是0
      min: 0,
      // 积分范围的最大值 少于多少积分，最多是10000，当传入0的时候，会直接返回所有商品，无视后台逻辑。
      max: 0,
      // 商品关键词  搜索关键字
      keyword: "",
      // 全部商品数据
      goodsData: [],
      // 显示部分索引
      goodsListShow: [],
      // 排序数据1
      options1: [
        { min: 0, max: 10000, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      // 排序数据2
      options2: [
        { type: 0, txt: "全部" },
        { type: 1, txt: "实体商品" },
        { type: 2, txt: "虚拟商品" },
      ],
      // 对应样式高亮索引
      op1Val: 0,
      // 对应样式高亮索引
      op2Val: 1,
      // 数据分页
      page: 1,
      // 一个多少条数据
      size: 8,
      // 是否已经滚动到底部
      isReachBottom: false,
    };
  },

  async created() {
    // let res = await allGoodsAPI({
    //   did: this.did,
    //   type: this.type,
    //   min: this.min,
    //   max: this.max,
    //   keyword: this.keyword,
    // });
    // console.log("全部商品列表页", res);
    // this.goodsData = res.data;
    this.keyword = this.$route.query.keyword || "";
    this.getGoodsData();
    // 初始化
    this.page = 1;
  },
  mounted() {
    window.addEventListener("scroll", this.scrollFn);
    console.log("监听滚动");
  },
  destroyed() {
    window.removeEventListener("scroll", this.scrollFn);
    console.log("跳出监听滚动");
  },
  components: {
    Crumb,
    List,
  },
  // 监听keyword,发生变化时候,触发created  ??原理
  watch: {
    "$route.query.keyword": {
      handler(newVal, oldVal) {
        this.keyword = newVal;
        this.getGoodsData();
      },
    },
  },

  methods: {
    op1Fn(index, min, max) {
      this.op1Val = index;
      this.min = min;
      this.max = max;
      this.keyword = "";
      this.getGoodsData();
      console.log(this.min, this.max);
    },
    op2Fn(index, type) {
      this.op2Val = index;
      this.type = type;
      this.keyword = "";

      // console.log(this.type);
      this.getGoodsData();
    },
    async getGoodsData() {
      let res = await allGoodsAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      this.goodsData = res.data;
      // console.log("全部商品列表页", res.data);
      // 筛选出8个数据显示出来
      this.goodsListShow = this.goodsData.filter((item, index) => {
        return index < 8;
      });
    },
    scrollFn() {
      if (getScrollTop() + getClientHeight() >= getScrollHeight() - 50) {
        console.log("***即将加载数据");
        // console.log(
        //   "滚动了",
        //   getScrollTop(),
        //   getClientHeight(),
        //   getScrollHeight()
        // );
        if (this.goodsListShow.length >= this.goodsData.length || "") {
          this.isReachBottom = true;
          return;
        }
        if (!this.isReachBottom) {
          this.isLoading = true;
          this.page++;
          this.goodsListShow = this.goodsData.filter(
            (item, index) => index < this.page * this.size
          );
          this.isReachBottom = false;
        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
</style>
